program nllambda_spec3_co3_mex

    version 13
	
	***RESTRICT AND SORT SAMPLE EVEN BEFORE ENTERING HERE!
    
    syntax varlist(min=171 max=171) [aw fw iw] if, at(name)
    local pref: word 1 of `varlist'
	local gr1941: word 2 of `varlist'
	local gr1942: word 3 of `varlist'
	local gr1943: word 4 of `varlist'
	local gr1944: word 5 of `varlist'
	local gr1945: word 6 of `varlist'
	local gr1946: word 7 of `varlist'
	local gr1947: word 8 of `varlist'
	local gr1948: word 9 of `varlist'
	local gr1949: word 10 of `varlist'
	local gr1950: word 11 of `varlist'
	local gr1951: word 12 of `varlist'
	local gr1952: word 13 of `varlist'
	local gr1953: word 14 of `varlist'
	local gr1954: word 15 of `varlist'
	local gr1955: word 16 of `varlist'
	local gr1956: word 17 of `varlist'
	local gr1957: word 18 of `varlist'
	local gr1958: word 19 of `varlist'
	local gr1959: word 20 of `varlist'
	local gr1960: word 21 of `varlist'
	local gr1961: word 22 of `varlist'
	local gr1962: word 23 of `varlist'
	local gr1963: word 24 of `varlist'
	local gr1964: word 25 of `varlist'
	local gr1965: word 26 of `varlist'
	local gr1966: word 27 of `varlist'
	local gr1967: word 28 of `varlist'
	local gr1968: word 29 of `varlist'
	local gr1969: word 30 of `varlist'
	local gr1970: word 31 of `varlist'
	local gr1971: word 32 of `varlist'
	local gr1972: word 33 of `varlist'
	local gr1973: word 34 of `varlist'
	local gr1974: word 35 of `varlist'
	local gr1975: word 36 of `varlist'
	local gr1976: word 37 of `varlist'
    local gr1977: word 38 of `varlist'
	local gr1978: word 39 of `varlist'
	local gr1979: word 40 of `varlist'
	local gr1980: word 41 of `varlist'
	local gr1981: word 42 of `varlist'
	local gr1982: word 43 of `varlist'
	local gr1983: word 44 of `varlist'
	local gr1984: word 45 of `varlist'
	local gr1985: word 46 of `varlist'
	local gr1986: word 47 of `varlist'
	local gr1987: word 48 of `varlist'
	local gr1988: word 49 of `varlist'
	local gr1989: word 50 of `varlist'
	local gr1990: word 51 of `varlist'
	local gr1991: word 52 of `varlist'
	local gr1992: word 53 of `varlist'
	local gr1993: word 54 of `varlist'
	local gr1994: word 55 of `varlist'
	local gr1995: word 56 of `varlist'
	local gr1996: word 57 of `varlist'
	local gr1997: word 58 of `varlist'
	local gr1998: word 59 of `varlist'
	local gr1999: word 60 of `varlist'
	local gr2000: word 61 of `varlist'
	local gr2001: word 62 of `varlist'
    local gr2002: word 63 of `varlist'
	local gr2003: word 64 of `varlist'
	local gr2004: word 65 of `varlist'
	local gr2005: word 66 of `varlist'
	local gr2006: word 67 of `varlist'
	local gr2007: word 68 of `varlist'
	local gr2008: word 69 of `varlist'
	local gr2009: word 70 of `varlist'
	local gr2010: word 71 of `varlist'
	local gr2011: word 72 of `varlist'
	local gr2012: word 73 of `varlist'
	local gr2013: word 74 of `varlist'
	local gr2014: word 75 of `varlist'
    local inflat: word 76 of `varlist'
    local ageit2: word 77 of `varlist'
	local ageit3: word 78 of `varlist'
    local biryear: word 79 of `varlist'
	local yrmx2: word 80 of `varlist'
	local yrmx3: word 81 of `varlist'
	local grmex1925: word 82 of `varlist'
	local grmex1926: word 83 of `varlist'
	local grmex1927: word 84 of `varlist'
	local grmex1928: word 85 of `varlist'
	local grmex1929: word 86 of `varlist'
	local grmex1930: word 87 of `varlist'
	local grmex1931: word 88 of `varlist'
	local grmex1932: word 89 of `varlist'
	local grmex1933: word 90 of `varlist'
	local grmex1934: word 91 of `varlist'
	local grmex1935: word 92 of `varlist'
	local grmex1936: word 93 of `varlist'	
	local grmex1937: word 94 of `varlist'
	local grmex1938: word 95 of `varlist'
	local grmex1939: word 96 of `varlist'
	local grmex1940: word 97 of `varlist'
	local grmex1941: word 98 of `varlist'
	local grmex1942: word 99 of `varlist'
	local grmex1943: word 100 of `varlist'
	local grmex1944: word 101 of `varlist'
	local grmex1945: word 102 of `varlist'
	local grmex1946: word 103 of `varlist'
	local grmex1947: word 104 of `varlist'
	local grmex1948: word 105 of `varlist'
	local grmex1949: word 106 of `varlist'
	local grmex1950: word 107 of `varlist'
	local grmex1951: word 108 of `varlist'
	local grmex1952: word 109 of `varlist'
	local grmex1953: word 110 of `varlist'
	local grmex1954: word 111 of `varlist'
	local grmex1955: word 112 of `varlist'
	local grmex1956: word 113 of `varlist'
	local grmex1957: word 114 of `varlist'
	local grmex1958: word 115 of `varlist'
	local grmex1959: word 116 of `varlist'
	local grmex1960: word 117 of `varlist'
	local grmex1961: word 118 of `varlist'
	local grmex1962: word 119 of `varlist'
	local grmex1963: word 120 of `varlist'
	local grmex1964: word 121 of `varlist'
	local grmex1965: word 122 of `varlist'
	local grmex1966: word 123 of `varlist'
	local grmex1967: word 124 of `varlist'
	local grmex1968: word 125 of `varlist'
	local grmex1969: word 126 of `varlist'
	local grmex1970: word 127 of `varlist'
	local grmex1971: word 128 of `varlist'
	local grmex1972: word 129 of `varlist'
	local grmex1973: word 130 of `varlist'
	local grmex1974: word 131 of `varlist'
	local grmex1975: word 132 of `varlist'
	local grmex1976: word 133 of `varlist'
	local grmex1977: word 134 of `varlist'
	local grmex1978: word 135 of `varlist'
	local grmex1979: word 136 of `varlist'
	local grmex1980: word 137 of `varlist'
	local grmex1981: word 138 of `varlist'
	local grmex1982: word 139 of `varlist'
	local grmex1983: word 140 of `varlist'
	local grmex1984: word 141 of `varlist'
	local grmex1985: word 142 of `varlist'
	local grmex1986: word 143 of `varlist'
	local grmex1987: word 144 of `varlist'
	local grmex1988: word 145 of `varlist'
	local grmex1989: word 146 of `varlist'
	local grmex1990: word 147 of `varlist'
	local grmex1991: word 148 of `varlist'
	local grmex1992: word 149 of `varlist'
	local grmex1993: word 150 of `varlist'
	local grmex1994: word 151 of `varlist'
	local grmex1995: word 152 of `varlist'
	local grmex1996: word 153 of `varlist'
	local grmex1997: word 154 of `varlist'
	local grmex1998: word 155 of `varlist'
	local grmex1999: word 156 of `varlist'
	local grmex2000: word 157 of `varlist'
	local grmex2001: word 158 of `varlist'
	local grmex2002: word 159 of `varlist'
	local grmex2003: word 160 of `varlist'
	local grmex2004: word 161 of `varlist'
	local grmex2005: word 162 of `varlist'
	local grmex2006: word 163 of `varlist'
	local grmex2007: word 164 of `varlist'
	local grmex2008: word 165 of `varlist'
	local grmex2009: word 166 of `varlist'
	local grmex2010: word 167 of `varlist'
	local grmex2011: word 168 of `varlist'
	local grmex2012: word 169 of `varlist'
	local grmex2013: word 170 of `varlist'
	local grmex2014: word 171 of `varlist'


    // Retrieve parameters out of at
    tempname bcons bgr bsd lam binf
    scalar `bcons' = `at'[1,1]
    scalar `bgr' = `at'[1,2]
	scalar `bsd' = `at'[1,3]
    scalar `lam' = `at'[1,4]
	scalar `binf' = `at'[1,5]


	`if' {
	
	forval mxflsi=2/3 {
	
	****Year max
	if `mxflsi'==2 {
	local yrmax=2006
	}
	
	if `mxflsi'==5 {
	local yrmax=2012
	}
	
		
	forval yr=1925/`yrmax'{
    tempvar weight_int`yr'_`mxflsi'
	local k=`yrmax'-`yr'
	generate double `weight_int`yr'_`mxflsi''=((`ageit`mxflsi''-`k')/(`ageit`mxflsi''))^`lam'
    replace `weight_int`yr'_`mxflsi''=. if `k'>`ageit`mxflsi'' | `biryear'>`yr' | `yrmx`mxflsi''<`yr' | `biryear'<1925
	
	*****
	
	
	}
	
	
	local hola `weight_int1925_`mxflsi''
	forval yr=1926/`yrmax'{
	local hola `hola' `weight_int`yr'_`mxflsi''
	}
	
	tempvar weightsum`mxflsi'
	egen `weightsum`mxflsi''=rowtotal(`hola'), missing
	
	forval yr=1925/`yrmax'{
    tempvar weight`yr'_`mxflsi'
	generate double `weight`yr'_`mxflsi''=`weight_int`yr'_`mxflsi''/`weightsum`mxflsi''
	}
	
	

	
	****Now multiply each weight by growth
	****Remember want to use from age 0-15 mex growth
	
	forval yr=1925/`yrmax'{
	
	tempvar weightgrowth`yr'_`mxflsi'
	
	generate double `weightgrowth`yr'_`mxflsi''=.
	
	if `yr'>=1941 {
	
    replace `weightgrowth`yr'_`mxflsi''=`weight`yr'_`mxflsi''*`gr`yr'' if `yr'-`biryear'>15
	replace `weightgrowth`yr'_`mxflsi''=`weight`yr'_`mxflsi''*`grmex`yr'' if `yr'-`biryear'<=15
	
	local k=`yrmax'-`yr'
	replace  `weightgrowth`yr'_`mxflsi''=. if `k'>`ageit`mxflsi'' | `biryear'>`yr' | `yrmx`mxflsi''<`yr' | `biryear'<1925
	}
	
	if `yr'<1941 {
	
	replace `weightgrowth`yr'_`mxflsi''=`weight`yr'_`mxflsi''*`grmex`yr'' if `yr'-`biryear'<=15
	
	local k=`yrmax'-`yr'
	replace  `weightgrowth`yr'_`mxflsi''=. if `k'>`ageit`mxflsi'' | `biryear'>`yr' | `yrmx`mxflsi''<`yr' | `biryear'<1925
	}
	

	
	}
	
	local hola `weightgrowth1925_`mxflsi''
	forval yr=1926/`yrmax'{
	local hola `hola' `weightgrowth`yr'_`mxflsi''
	}
	

    tempvar gruse_`mxflsi'
	egen `gruse_`mxflsi''=rowtotal(`hola'), missing
	
	
	********************SD*******************
	
	forval yr=1925/`yrmax'{
	
	
		if `yr'-`biryear'<=15 {
	local grnam  grmex
	}
	if `yr'-`biryear'>15 {
	local grnam gr
	}
	
	tempvar eachnumSD`yr'_`mxflsi'
    generate double `eachnumSD`yr'_`mxflsi''=.
	
	
	if `yr'>=1941 {
	
	
	replace `eachnumSD`yr'_`mxflsi''=`weight_int`yr'_`mxflsi''*(`gr`yr''-`gruse_`mxflsi'')^2 if `yr'-`biryear'>15
    replace `eachnumSD`yr'_`mxflsi''=`weight_int`yr'_`mxflsi''*(`grmex`yr''-`gruse_`mxflsi'')^2 if `yr'-`biryear'<=15
	
	
	local k=`yrmax'-`yr'
	replace `eachnumSD`yr'_`mxflsi''=. if `k'>`ageit`mxflsi'' | `biryear'>`yr' | `yrmx`mxflsi''<`yr'
}

	if `yr'<1941 {
	
	
    replace `eachnumSD`yr'_`mxflsi''=`weight_int`yr'_`mxflsi''*(`grmex`yr''-`gruse_`mxflsi'')^2 if `yr'-`biryear'<=15
	
	
	local k=`yrmax'-`yr'
	replace `eachnumSD`yr'_`mxflsi''=. if `k'>`ageit`mxflsi'' | `biryear'>`yr' | `yrmx`mxflsi''<`yr'
}


}

***Add up numerator for SD

	local hola `eachnumSD1925_`mxflsi''
	forval yr=1926/`yrmax'{
	local hola `hola' `eachnumSD`yr'_`mxflsi''
	}

tempvar numtotalSD_`mxflsi'
egen `numtotalSD_`mxflsi''=rowtotal(`hola'), missing

****Build sd

tempvar sduse_`mxflsi'
generate double `sduse_`mxflsi''=sqrt((`ageit`mxflsi''-1)*`numtotalSD_`mxflsi''/((`ageit`mxflsi''-2)*`weightsum`mxflsi''))

	
	}


	tempvar grusedif
	gen double `grusedif' = `gruse_3' - `gruse_2'


	
	tempvar sdusedif
	gen double `sdusedif' = `sduse_3' - `sduse_2'

	
	// Now fill in dependent variable
    replace  `pref' = `bcons' +`bgr'*`grusedif'+ `bsd'*`sdusedif' +`binf'*`inflat' 


	
	}
	

end
